﻿using System;

namespace OMC.DataManagement
{
    /// <summary>
    /// This class represents a MailItem in Outlook.
    /// </summary>
    [Serializable()]
    public class MailContent : IClassifiable
    {
        private String body;
        private String subject;
        private String from;
        private String to;
        private static LuceneAnalyzer luceneAnalyzer = new LuceneAnalyzer();
        private static bool useStopwords = true;

        /// <summary>
        /// The constructor.
        /// </summary>
        /// <param name="body">body of the mail</param>
        /// <param name="subject">subject of the mail</param>
        /// <param name="from">sender adress of the mail</param>
        /// <param name="to">reciever adress of the mail</param>
        public MailContent(String body, String subject, String from, String to)
        {
            this.body = GetAnalyzedString(body);
            this.subject = GetAnalyzedString(subject);
            this.from = GetAnalyzedString(from);
            this.to = GetAnalyzedString(to);
        }

        /// <summary>
        /// This method builds an analyzed string using the apache.lucene analyzer.
        /// </summary>
        /// <param name="text">the String to analyze</param>
        /// <returns>the analyzed string</returns>
        private String GetAnalyzedString(String text)
        {
            if (text == null)
            {
                text = "";
            }
            text.Replace('ä', 'a');
            text.Replace('ö', 'o');
            text.Replace('ü', 'u');
            if (useStopwords)
            {
                text = luceneAnalyzer.GetAnalyzedTextUsingStopwords(text);
            }
            else
            {
                text = luceneAnalyzer.GetAnalyzedText(text);
            }
            text = text.Replace("class", ""); //avoid having the word "class", because it makes trouble
            return text;
        }

        /// <summary>
        /// This method sets the stopwords of enable or disable.
        /// </summary>
        /// <param name="isUsed">
        /// true, if the Analyzer should used
        /// false, to ignore the Analyzer
        /// </param>
        public static void SetUseStopwords(bool isUsed)
        {
            useStopwords = isUsed;
        }

        /// <summary>
        /// This method gets the content of a MailContent object
        /// </summary>
        /// <returns>String of the Content</returns>
        public String GetContent()
        {
            return subject + " " + body;
        }
    }
}